package edu.ynu.se.xiecheng.achitectureclass.project.controller;

import edu.ynu.se.xiecheng.achitectureclass.common.controller.LogicController;
import edu.ynu.se.xiecheng.achitectureclass.project.dao.UserDAO;
import edu.ynu.se.xiecheng.achitectureclass.project.po.dto.LoginDTO;
import edu.ynu.se.xiecheng.achitectureclass.project.po.entity.User;
import edu.ynu.se.xiecheng.achitectureclass.project.service.UserService;
import edu.ynu.se.xiecheng.achitectureclass.project.utils.JwtUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
@Api(tags = "用户管理")
@Slf4j
public class UserController extends LogicController<UserService, UserDAO, User, Long> {
    public UserController(UserService ls) {
        super(ls);
    }
    @PostMapping("/login")
    @ApiOperation("用户登录")
    public ResponseEntity<?> login(@RequestBody LoginDTO loginRequest) {
        try {
            // 验证用户名密码
            User user = service.validateUser(loginRequest.getUsername(), loginRequest.getPassword());

            if (user != null) {
                // 创建JWT载荷
                Map<String, Object> claims = new HashMap<>();
                claims.put("id", user.getId());
                claims.put("username", user.getUsername());

                // 生成JWT令牌
                String token = JwtUtils.generateJwt(claims);
                log.info(" JWT token: {}", token);

                // 构建响应
                Map<String, Object> response = new HashMap<>();
                response.put("token", token);
                response.put("user", user);

                return ResponseEntity.ok(response);
            } else {
                return ResponseEntity.status(HttpStatus.UNAUTHORIZED)
                        .body("用户名或密码错误");
            }
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body("登录失败：" + e.getMessage());
        }
    }

    @PostMapping("/register")
    @ApiOperation("用户注册")
    public ResponseEntity<?> register(@RequestBody LoginDTO registerRequest) {
        try {
            // 检查用户名是否已存在
            if (service.existsByUsername(registerRequest.getUsername())) {
                return ResponseEntity.badRequest()
                        .body("用户名已存在");
            }

            // 创建新用户
            User newUser = new User();
            newUser.setUsername(registerRequest.getUsername());
            newUser.setPassword(registerRequest.getPassword()); // 实际应用中应该加密

            // 保存用户
            User user = service.save(newUser);

            if (user != null){
            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("message", "注册成功");

            return ResponseEntity.ok(response);
            }
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body("注册失败：" + e.getMessage());
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                .body("注册失败" );
    }
}
